//
//  StatusCell.m
//  微博2期
//
//  Created by J on 15/11/29.
//
//

#import "StatusCell.h"
#import "StatusFrame.h"
#import "User.h"
#import "Status.h"
#import "photo.h"
#import "StatusToolbar.h"//底部tollbar

@interface StatusCell()

/**
 *  原创微博整体
 */
@property(nonatomic,weak)UIView * originalView;

/**
 *  头像
 */
@property(nonatomic,weak)UIImageView * iconView;
/**
 *  会员图标
 */
@property(nonatomic,weak)UIImageView * vipView;
/**
 *  配图
 */
@property(nonatomic,weak)UIImageView * photoView;

/**
 *  昵称
 */
@property(nonatomic,weak)UILabel * nameLabel;
/**
 *  时间
 */
@property(nonatomic,weak)UILabel * timeLabel;
/**
 *  来源
 */
@property(nonatomic,weak)UILabel * sourceLabel;

/**
 *  正文
 */
@property(nonatomic,weak)UILabel * contentLabel;








/**
 *  转发微博整体
 */
@property(nonatomic,weak)UIView * retweetView; //repost


/**
 *  转发微博正文 + 昵称
 */

@property(nonatomic,weak)UILabel * retweetContentLabel;


/**
 *  转发配图
 */
@property(nonatomic,weak)UIImageView * retweetPhotoView;

//** 工具条 */
@property(nonatomic,weak)StatusToolbar * toolbar;




@end
@implementation StatusCell


- (void)setStatusFrame:(StatusFrame *)statusFrame{
    
    _statusFrame = statusFrame;
    
    Status * status = statusFrame.status;//取出用户模型
    User * user = status.user;//取出用户
    
    //1.原创微博整体
       self.originalView.frame = statusFrame.originalViewF;
    /**
     *  头像
     */
    self.iconView.frame = statusFrame.iconViewF;
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:user.profile_image_url] placeholderImage:[UIImage imageNamed:@"avatar_default_small"]];
    
    /**
     *  会员图标
     */
    if (user.isVip) {//是会员
        self.vipView.hidden = NO;
        self.vipView.frame = statusFrame.vipViewF;
        self.vipView.image = [UIImage imageNamed:[NSString stringWithFormat:@"common_icon_membership_level%d",user.mbrank]];
        self.nameLabel.textColor = [UIColor orangeColor];
    }else{
        self.nameLabel.textColor = [UIColor blackColor];
        self.vipView.hidden = YES;
    }
    /**
     *  配图
     */
    self.photoView.frame = statusFrame.photoViewF;
    if (status.pic_urls.count) {
        self.photoView.hidden = NO;

        Photo * photo = status.pic_urls[0];
        [self.photoView sd_setImageWithURL:[NSURL URLWithString:photo.thumbnail_pic] placeholderImage:[UIImage imageNamed:@"timeline_image_placeholder"]];
    }else{
        self.photoView.hidden = YES;
    }
    
    /**
     *  昵称
     */
    self.nameLabel.frame = statusFrame.nameLabelF;
    self.nameLabel.text = status.user.name;
    /**
     *  时间
     */
    self.timeLabel.frame = statusFrame.timeLabelF;
    self.timeLabel.text = status.created_at;

    /**
     *  来源
     */
    self.sourceLabel.frame = statusFrame.sourceLabelF;
    self.sourceLabel.text = status.source;
    /**
     *  正文
     */
    self.contentLabel.frame= statusFrame.contentLabelF;
    self.contentLabel.text = status.text;
    
    
   /** 被转发微博*/
    if (status.retweeted_status) {
        
        Status * retweeted_status = status.retweeted_status;
        User * retweeted_user = retweeted_status.user;
        
        
        self.retweetView.hidden = NO;
        self.retweetView.frame = statusFrame.retweetViewF;
        
        /**
         *  被转发微博正文
         */
        
        NSString * retweetContent = [NSString stringWithFormat:@"@%@ : %@",retweeted_user.name,retweeted_status.text];
        self.retweetContentLabel.text = retweetContent;
        self.retweetContentLabel.frame= statusFrame.retweetContentLabelF;
        
        
        
        /**
         *  被转发微博配图
         */
        self.photoView.frame = statusFrame.photoViewF;
        if (retweeted_status.pic_urls.count) {
            self.retweetPhotoView.frame = statusFrame.retweetPhotoViewF;
            Photo * retweetPhotoView = retweeted_status.pic_urls[0];
            [self.retweetPhotoView sd_setImageWithURL:[NSURL URLWithString:retweetPhotoView.thumbnail_pic] placeholderImage:[UIImage imageNamed:@"timeline_image_placeholder"]];
        }

        
        
        
        
    }else{
        self.retweetView.hidden = YES;
    }
    /** 整体*/
    self.toolbar.frame = statusFrame.toolbarF;
    self.toolbar.status = status;//工具条数据


    
    

    
    
}

+(instancetype)cellWithTableView:(UITableView*)tableview{
    StatusCell * cell = [tableview dequeueReusableCellWithIdentifier:@"ID"];
    if (!cell) {
        cell = [[StatusCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"ID"];
    }
    return cell;
}

//cell的初始化方法, 一个cell只会调用一次
//一般这里添加所有可能显示的子控件 以及子控件的一次性设置
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];//设置cell背景色为透明
//        self.selectionStyle = UITableViewCellSeparatorStyleNone;//这行和自定义的背景颜色不能同时出现否则自定义颜色无效
        
//        UIView * bg = [[UIView alloc]initWithFrame:self.frame];
//        bg.backgroundColor = [UIColor orangeColor];
//        self.selectedBackgroundView = bg;
        
        //初始化原创微博
        [self setupOriginal];
      
        //初始化转发微博
        [self setupRetweet];
        
        
        //初始化工具条
        [self setupToolbar];
        

    }
    
    
    return self;
    
}


/**
 *  初始化工具条
 */
- (void)setupToolbar{
    StatusToolbar * toolbar = [[StatusToolbar alloc]init];
//    toolbar.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:toolbar];
    self.toolbar = toolbar;
}

/**
 *       初始化转发微博

 */
- (void)setupRetweet{
    
    
    
    //1.转发微博微博整体
    UIView * retweetView = [[UIView alloc]init];
    [self.contentView addSubview:retweetView];
    self.retweetView = retweetView;
    
    retweetView.backgroundColor = Color(247, 247, 247);
 
    
    /**
     正文 + 昵称
     */
    UILabel * retweetContentLabel = [[UILabel alloc]init];
    retweetContentLabel.numberOfLines = 0;
    retweetContentLabel.font = StatusCellReweetContentFont;
    [self.retweetView addSubview:retweetContentLabel];
    self.retweetContentLabel = retweetContentLabel;
    

 
    /**
     *  配图
     */
    UIImageView * retweetPhotoView = [[UIImageView alloc]init];
    retweetPhotoView.contentMode = UIViewContentModeCenter;
    [self.retweetView addSubview:retweetPhotoView];
    self.retweetPhotoView = retweetPhotoView;
    
    
    
}

//- (void)setFrame:(CGRect)frame{
//    
//    frame.origin.y += StatusCellMargin;//所有cell y 下移 15
//    [super setFrame:frame];
//}

- (void)setupOriginal{
    //1.原创微博整体
    
    UIView * originalView = [[UIView alloc]init];
    [self.contentView addSubview:originalView];
    self.originalView = originalView;//原创微博背景色
    originalView.backgroundColor = [UIColor whiteColor];
    UIImageView * iconView = [[UIImageView alloc]init];
    [self.originalView addSubview:iconView];
    self.iconView = iconView;
    
    /**
     *  会员图标
     */
    UIImageView * vipView = [[UIImageView alloc]init];
    vipView.contentMode = UIViewContentModeCenter;
    [self.originalView addSubview:vipView];
    self.vipView = vipView;
    
    
    UIImageView * photoView = [[UIImageView alloc]init];
    [self.originalView addSubview:photoView];
    self.photoView = photoView;
    
    
    UILabel * nameLabel = [[UILabel alloc]init];
    [self.originalView addSubview:nameLabel];
    nameLabel.font = StatusCellNameFont;
    self.nameLabel = nameLabel;
    
    /**
     *  发布时间
     */
    UILabel * timeLabel = [[UILabel alloc]init];
    [self.originalView addSubview:timeLabel];
    timeLabel.font = StatusCellTimeFont;
    self.timeLabel = timeLabel;
    
    
    UILabel * sourceLabel = [[UILabel alloc]init];
    [self.contentView addSubview:sourceLabel];
    self.sourceLabel = sourceLabel;
    
    /**
     正文
     */
    UILabel * contentLabel = [[UILabel alloc]init];
    contentLabel.numberOfLines = 0;
    contentLabel.font = StatusContentFont;
    [self.originalView addSubview:contentLabel];
    self.contentLabel = contentLabel;
    
    
}

@end
